//
//  UIImage+Extend.m
//  WOM
//
//  Created by leeqb on 3/13/14.
//  Copyright (c) 2014 cst. All rights reserved.
//

#import "UIImage+Extend.h"

@implementation UIImage (Extend)

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [color set];
    UIRectFill(CGRectMake(0, 0, size.width, size.height));
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return colorImage;
}

+ (UIImage *)scaleImage:(UIImage *)image scale:(CGFloat)scale
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scale, image.size.height * scale)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return scaledImage;
}

+ (UIImage *)getDownloadDefaultImage:(CGSize)imgSize
{
    UIGraphicsBeginImageContextWithOptions(imgSize, YES, 2);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, RGB(234, 234, 234).CGColor);
    CGPoint pts[] = {CGPointMake(0, 0), CGPointMake(imgSize.width, 0),
        CGPointMake(imgSize.width, imgSize.height), CGPointMake(0, imgSize.height)};
    CGContextAddLines(ctx, pts, sizeof(pts)/sizeof(CGPoint));
    CGContextFillPath(ctx);
    UIImage *defImage = [UIImage imageNamed:@"def_img_download.png"];
    CGRect rect = CGRectZero;
    rect.size.width = imgSize.width / 3;
    rect.size.height = imgSize.height / 3;
    rect.origin.x = (imgSize.width - rect.size.width) / 2;
    rect.origin.y = (imgSize.height - rect.size.height) / 2;
    [defImage drawInRect:rect];
    defImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return defImage;
}

// 获取某一像素点的颜色值
- (UIColor *)colorAtPixel:(CGPoint)point {
    // Cancel if point is outside image coordinates
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
        return nil;
    }
    
    // Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
    // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.CGImage;
    NSUInteger width = CGImageGetWidth(cgImage) / 2;
    NSUInteger height = CGImageGetHeight(cgImage) / 2;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,
                                                 1,
                                                 1,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    
    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, -pointY);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);
    
    // Convert color values [0..255] to floats [0.0..1.0]
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

#pragma mark 获得自定义虚线
+ (UIImageView *)imageLineDashRow:(CGRect)frame len:(int)len gap:(int)gap color:(UIColor *)c
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    UIGraphicsBeginImageContext(frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGFloat lengths[] = {len, gap};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGColorRef color = c.CGColor;
    CGContextSetStrokeColorWithColor(line, color);
    CGContextSetLineDash(line, 0, lengths, 2);
    CGContextMoveToPoint(line, 0, frame.size.height);
    CGContextAddLineToPoint(line, frame.size.width, frame.size.height);
    CGContextStrokePath(line);
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    return imageView;
}

#pragma mark 获得自定义虚线
+ (UIImageView *)imageLineDashCol:(CGRect)frame len:(int)len gap:(int)gap color:(UIColor *)c
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    UIGraphicsBeginImageContext(frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGFloat lengths[] = {len, gap};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGColorRef color = c.CGColor;
    CGContextSetStrokeColorWithColor(line, color);
    CGContextSetLineDash(line, 0, lengths, 2);
    CGContextMoveToPoint(line, frame.size.width, 0);
    CGContextAddLineToPoint(line, frame.size.width, frame.size.height);
    CGContextStrokePath(line);
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    return imageView;
}

+ (UIImage *)getDefaultImageInSize:(CGSize)size
{
    UIImage *defImage = [UIImage imageNamed:@"icon_default_image.png"];
    CGRect imgRect = CGRectZero;
    imgRect.size.width = defImage.size.width;
    imgRect.size.height = defImage.size.height;
    imgRect.origin.x = (size.width - imgRect.size.width) / 2;
    imgRect.origin.y = (size.height - imgRect.size.height) / 2;
    UIGraphicsBeginImageContextWithOptions(size, YES, 2);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(ctx, RGB(238, 238, 238).CGColor);
    CGContextFillRect(ctx, CGRectMake(0, 0, size.width, size.height));
    [defImage drawInRect:imgRect blendMode:kCGBlendModeNormal alpha:1];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return destImage;
}

@end
